package com.baidu.ueditor.utils;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 来自于 : http://blog.csdn.net/zengshunyao/article/details/44758297
 * 在原作者的基础上面有一定的更新
 *
 * @author Jerry.X.He
 * @version 1.0
 * @date 2016-11-01 15:56:26
 */

/*******************************************************************************
 * Description: 图片水印工具类 
 * @author zengshunyao
 * @version 1.0
 */
public class MarkImageUtils {

    // 水印透明度
    private static float alpha = 0.5f;
    // 水印横向位置
    private static int positionX = 150;
    // 水印纵向位置
    private static int positionY = 300;
    // 水印宽度
    private static int coverWidth = 300;
    // 水印高度
    private static int coverHeight = 200;
    // 水印文字字体
    private static Font font = new Font("宋体", Font.BOLD, 72);
    // 水印文字颜色
    private static Color color = Color.red;

    /**
     *
     * @param alpha
     *            水印透明度
     * @param positionX
     *            水印横向位置
     * @param positionY
     *            水印纵向位置
     * @param font
     *            水印文字字体
     * @param color
     *            水印文字颜色
     */
    public static void setImageMarkOptions(float alpha, int positionX,
                                           int positionY, int coverWidth, int coverHeight, Font font, Color color) {
        if (alpha != 0.0f)
            MarkImageUtils.alpha = alpha;
        if (positionX != 0)
            MarkImageUtils.positionX = positionX;
        if (positionY != 0)
            MarkImageUtils.positionY = positionY;
        if (coverWidth != 0)
            MarkImageUtils.coverWidth = coverWidth;
        if (coverHeight != 0)
            MarkImageUtils.coverHeight = coverHeight;
        if (font != null)
            MarkImageUtils.font = font;
        if (color != null)
            MarkImageUtils.color = color;
    }

    // ----------------------------------------- 添加水印相关 -------------------------------------------------    

    public static void markImageByIcon(String iconPath, String srcImgPath,
                                       String targetPath) {
        markImageByIcon(iconPath, srcImgPath, targetPath, null);
    }

    public static void markImageByIcon(BufferedImage icon, BufferedImage srcImg, String targetPath) {
        markImageByIcon(icon, srcImg, targetPath, null);
    }

    public static void markImageByIcon(String iconPath, String srcImgPath,
                                       String targetPath, Integer degree) {
        try {
            BufferedImage icon = ImageIO.read(new File(iconPath));
            BufferedImage srcImg = ImageIO.read(new File(srcImgPath));
            markImageByIcon(icon, srcImg, targetPath, degree);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void markImageByIcon(BufferedImage icon, BufferedImage srcImg,
                                       String targetPath, Integer degree) {
        markImageByIcon(icon, srcImg, targetPath, degree, positionY, positionX, coverWidth, coverHeight);
    }

    public static void markImageByIcon(String iconPath, String srcImgPath,
                                       String targerPath, Integer degree, int x, int y, int width, int height) {
        try {
            BufferedImage icon = ImageIO.read(new File(iconPath));
            BufferedImage srcImg = ImageIO.read(new File(srcImgPath));
            markImageByIcon(icon, srcImg, targerPath, degree, x, y, width, height);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 给图片添加水印图片、可设置水印图片旋转角度
     *
     * @param icon
     *            水印图片路径
     * @param srcImg
     *            源图片
     * @param targerPath
     *            目标图片路径
     * @param degree
     *            水印图片旋转角度
     * @param x
     * @param y
     * 			水印的位置
     * @param width
     * @param height
     * 			水印的宽高   
     */
    public static void markImageByIcon(BufferedImage icon, BufferedImage srcImg,
                                       String targerPath, Integer degree, int x, int y, int width, int height) {
        OutputStream os = null;
        try {
            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
                    srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);

            // 1、得到画笔对象
            Graphics2D g = buffImg.createGraphics();

            // 2、设置对线段的锯齿状边缘处理
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);

            g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null),
                    srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);
            // 3、设置水印旋转
            if (null != degree) {
                g.rotate(Math.toRadians(degree),
                        (double) buffImg.getWidth() / 2,
                        (double) buffImg.getHeight() / 2);
            }

            // 4、水印图片的路径 水印图片一般为gif或者png的，这样可设置透明度
            ImageIcon imgIcon = new ImageIcon(icon);

            // 5、得到Image对象。
            Image img = imgIcon.getImage();

            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
                    alpha));

            // 6、水印图片的位置
            // g.drawImage(img, positionWidth, positionHeight, null);
            g.drawImage(img, x, y, width, height, null);

            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
            // 7、释放资源
            g.dispose();

            // 8、生成图片
            os = new FileOutputStream(targerPath);
            ImageIO.write(buffImg, "JPG", os);

//            System.out.println("图片完成添加水印图片");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != os)
                    os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    // ----------------------------------------- 添加文字相关 -------------------------------------------------

    public static void markImageByText(String logoText, String srcImgPath,
                                       String targerPath) {
        markImageByText(logoText, srcImgPath, targerPath, null);
    }

    public static void markImageByText(String logoText, BufferedImage srcImg,
                                       String targerPath) {
        markImageByText(logoText, srcImg, targerPath, null);
    }

    public static void markImageByText(String logoText, String srcImgPath,
                                       String targerPath, Integer degree) {
        try {
            BufferedImage srcImg = ImageIO.read(new File(srcImgPath));
            markImageByText(logoText, srcImg, targerPath, degree);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void markImageByText(String logoText, BufferedImage srcImg,
                                       String targerPath, Integer degree) {
        markImageByText(logoText, srcImg, targerPath, degree, positionX, positionY);
    }

    public static void markImageByText(String logoText, String srcImgPath,
                                       String targerPath, Integer degree, int x, int y) {
        try {
            BufferedImage srcImg = ImageIO.read(new File(srcImgPath));
            markImageByText(logoText, srcImg, targerPath, degree);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 给图片添加水印文字、可设置水印文字的旋转角度
     *
     * @param logoText
     * @param srcImg
     * @param targerPath
     * @param degree
     */
    public static void markImageByText(String logoText, BufferedImage srcImg,
                                       String targerPath, Integer degree, int x, int y) {
        InputStream is = null;
        OutputStream os = null;
        try {
            // 1、源图片
            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
                    srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);

            // 2、得到画笔对象
            Graphics2D g = buffImg.createGraphics();
            // 3、设置对线段的锯齿状边缘处理
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g.drawImage(
                    srcImg.getScaledInstance(srcImg.getWidth(null),
                            srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0,
                    null);
            // 4、设置水印旋转
            if (null != degree) {
                g.rotate(Math.toRadians(degree),
                        (double) buffImg.getWidth() / 2,
                        (double) buffImg.getHeight() / 2);
            }
            // 5、设置水印文字颜色
            g.setColor(color);
            // 6、设置水印文字Font
            g.setFont(font);
            // 7、设置水印文字透明度
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
                    alpha));
            // 8、第一参数->设置的内容，后面两个参数->文字在图片上的坐标位置(x,y)
            g.drawString(logoText, x, y);
            // 9、释放资源
            g.dispose();
            // 10、生成图片
            os = new FileOutputStream(targerPath);
            ImageIO.write(buffImg, "JPG", os);

//            System.out.println("图片完成添加水印文字");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != is)
                    is.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (null != os)
                    os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}